﻿using FlashGameSite.Manager;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace FlashGameSite
{
    public partial class FormForgotPassword : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void btnGetPassword_Click(object sender, EventArgs e)
        {
            using (FlashGamesDataContext dataContext = new FlashGamesDataContext())
            {
                int existingUserCount = dataContext.UserInfos.Where(obj => (obj.Username == txtUsername.Text && obj.EmailID == txtEmailID.Text)).Count();
                if (existingUserCount > 0)
                {
                    string newpassword = Guid.NewGuid().ToString().Substring(0, 8);
                    dataContext.ResetPassword(txtUsername.Text, newpassword);
                    Common.SendMail(txtEmailID.Text, "Password change notification", "");
                    ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "alert('New password is emailed to you.')", true);
                }
                else
                {
                    ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "alert('Cannot fetch the user info with the entered details. Please contact us via contact form.')", true);
                }
            }
        }

    }
}